function addStyles(win) {
  let style = win.document.createElement("style");
  style.type = "text/css";
  style.innerHTML = "";
  win.document.getElementsByTagName("head")[0].appendChild(style);
}

const VueHtmlToPaper = {
  install(Vue, option) {
    Vue.mixin({
      methods: {
        $htmlToPaper(el, opts = option) {
          let { name, specs, replace, styles } = opts;
          specs = specs.length ? specs.join(",") : "";
          const element = document.getElementById(el);
          if (!element) {
            alert(`Element to print #${el} not found!`);
            return;
          }
          const url = "";

          const win = window.open(url, name, specs, replace);

          win.document.write(`
            <html>
              <head>
                <title>${document.title}</title>
              </head>
              <body>
                ${element.innerHTML}
              </body>
            </html>
          `);

          addStyles(win, styles);

          setTimeout(() => {
            win.document.close();
            win.focus();
            win.print();
            win.close();
          }, 1000);
          return true;
        }
      }
    });
  }
};

export default VueHtmlToPaper;
